package kangbo.studentmanagementsystem;

import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.geometry.HPos;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;

import java.io.IOException;

public class Home_Childinterface  {

    /***Home_Children_face SIZE***/
    static int Width=600,Height=400;

    public static Stage Hintstage = new Stage();
    static HBox box = new HBox(20);
    /***添加学生****/
    static void AddMessage(String idText,String nameText){

        BorderPane borderPane = new BorderPane();
        Message_gridPane.setReadAndwrite(true);
        borderPane.setCenter(Message_gridPane.gridPane);
        BorderPane.setAlignment(Message_gridPane.gridPane, Pos.CENTER);
        Homepage.Addstage.setResizable(false);
        Scene scene = new Scene(borderPane, Width, Height);


        Message_gridPane.setReadAndwrite(true);
        Message_gridPane.ClearMessage();
        /**初始化输入文本*/
        Message_gridPane.ID.setText(idText);
        Message_gridPane.name.setText(nameText);
        /*添加按钮*/
        box.getChildren().clear();
        Button cancel = new Button("取消",new ImageView("file:ico/closel.png"));
        Button sure = new Button("确定",new ImageView("file:ico/correct.png"));
        sure.setPrefWidth(80);
        cancel.setPrefWidth(80);

        box.getChildren().addAll(cancel,sure);
        box.setAlignment(Pos.CENTER);

        Message_gridPane.gridPane.getChildren().remove(box);
        Message_gridPane.gridPane.add(box,0,4,4,1);
        Message_gridPane.gridPane.setHalignment(cancel, HPos.RIGHT);



        //按钮点击事件
        cancel.setOnAction((ActionEvent e) -> {
            Homepage.Addstage.close();
        });
        sure.setOnAction((ActionEvent e) -> {
            //获取当前表中信息
            Message_gridPane.RadioButtonmonitor();
            Student_message student_message = new Student_message();
            String s = Message_gridPane.getMassage();
//            System.out.println(s);
            String a[] = s.split(",");

            /*******************************************/
            boolean falg = true;//信息填写是否完整
            if(a.length<6)falg=false;
            for (int i = 0; i < a.length; i++) {
                if(a[i].equals("")){
                    falg=false;
                    break;
                }
            }
            /******************************************/


            if(falg){
                student_message.setMessage(s);
                if(Homepage.Contains(student_message)) {
                    Homepage.message.add(student_message);
//                    Homepage.data.add(student_message);
//                System.out.println(student_message.toString());
                    Homepage.Updatelist();//刷新
                    Cinsucceed();
                    Homepage.Addstage.close();

                }
                else {
                    Adderror();
                }//添加失败已有该学生
            }
            else {
                Cinerror();
            }//添加失败信息不全
        });

        Homepage.Addstage.setTitle("学生-新增");
        Homepage.Addstage.setScene(scene);
        Homepage.Addstage.getIcons().add(new Image("File:title/add.png"));
//        stage.initStyle(StageStyle.UTILITY);
        Homepage.Addstage.show();
    }

    /****修改信息*******/
    static  void  ReavmpMessage(Student_message student_message){

        if(student_message!=null)
            Message_gridPane.setMessage(student_message.toString());

        Message_gridPane.setReadAndwrite(true);

        BorderPane borderPane = new BorderPane();
        borderPane.setCenter(Message_gridPane.gridPane);
        BorderPane.setAlignment(Message_gridPane.gridPane, Pos.CENTER);
        Homepage.Reavmpstage.setResizable(false);
        Scene scene = new Scene(borderPane, Width, Height);



        /*添加按钮*/
        Button sure = new Button("确定",new ImageView("file:ico/correct.png"));
        Button del = new Button("取消",new ImageView("file:ico/closel.png"));
        sure.setPrefWidth(80);
        del.setPrefWidth(80);

        box.getChildren().clear();
        box.getChildren().addAll(del,sure);
        box.setAlignment(Pos.CENTER);

        Message_gridPane.gridPane.getChildren().remove(box);
        Message_gridPane.gridPane.add(box,0,4,4,1);
        Message_gridPane.gridPane.setHalignment(del, HPos.RIGHT);

        //按钮点击事件
        del.setOnAction((ActionEvent e) -> {
            Homepage.Reavmpstage.close();
        });
        sure.setOnAction((ActionEvent e) -> {
            //获取当前表中信息
            Message_gridPane.RadioButtonmonitor();//获取性别
            String s = Message_gridPane.getMassage();

//            System.out.println(s);
            String a[] = s.split(",");

            boolean falg = true;
            if(a.length==0)falg=false;
            for (int i = 0; i < a.length; i++) {
                if(a[i].equals("")){
                    falg=false;
                    break;
                }
            }

            if(falg){
                student_message.setMessage(s);
                Changuscceed();
                Homepage.Updatelist();
                Homepage.Reavmpstage.close();
            }
            else {
                Changeerror();
            }//修改失败
        });

        Homepage.Reavmpstage.setTitle("修改信息");
        Homepage.Reavmpstage.setScene(scene);
        Homepage.Reavmpstage.getIcons().add(new Image("File:title/modify.png"));
        Homepage.Reavmpstage.show();
    }


    /***传入查找到的学生*/
    static void ReviseMessage(Student_message student_message){
        if(student_message!=null)
        Message_gridPane.setMessage(student_message.toString());

        //不可修改
        Message_gridPane.setReadAndwrite(false);


        BorderPane borderPane = new BorderPane();
        borderPane.setCenter(Message_gridPane.gridPane);
        BorderPane.setAlignment(Message_gridPane.gridPane, Pos.CENTER);
        Homepage.Revisestage.setResizable(false);
        Scene scene = new Scene(borderPane, Width, Height);


        /*添加按钮*/

        Button sure = new Button("返回",new ImageView("file:ico/logout.png"));
        Button del = new Button("删除",new ImageView("file:ico/del.png"));
        Button modify = new Button("修改",new ImageView("file:ico/modify.png"));
        sure.setPrefWidth(80);
        del.setPrefWidth(80);
        modify.setPrefWidth(80);

        box.getChildren().clear();
        box.getChildren().addAll(modify,del,sure);
        box.setAlignment(Pos.CENTER);

        Message_gridPane.gridPane.getChildren().remove(box);
        Message_gridPane.gridPane.add(box,0,4,4,1);
        Message_gridPane.gridPane.setHalignment(del, HPos.RIGHT);



        //按钮点击事件
        del.setOnAction((ActionEvent e) -> {//删除
            Hintstage.setTitle("删除");
            Hintstage.setResizable(false);
            BorderPane borderPane1 = new BorderPane();

            Text text1 = new Text("确认删除该学生？");
            text1.setFont(new Font(30));
            text1.setFill(Paint.valueOf("RED"));
            HBox hBox = new HBox(40);
            hBox.setPrefHeight(30);
            Button sure1 = new Button("删除", new ImageView("File:ico/del.png"));
            Button back = new Button("返回", new ImageView("File:ico/logout.png"));
            hBox.getChildren().addAll(back, sure1);


            back.setOnAction((ActionEvent a) -> {
                Hintstage.close();
            });
            sure1.setOnAction((ActionEvent a) -> {
                Homepage.message.remove(student_message);
                Homepage.Updatelist();
                Homepage.DeleteSure();
                Hintstage.close();
                Homepage.Revisestage.close();
            });

             borderPane1.setCenter(text1);
//        text1.setAlignment(Pos.CENTER);
            borderPane1.setBottom(hBox);
            hBox.setAlignment(Pos.CENTER);
            Scene scene21 = new Scene(borderPane1,400,300);
            Hintstage.getIcons().add(new Image("File:title/error.png"));
            Hintstage.setScene(scene21);
            Hintstage.show();

        });


        sure.setOnAction((ActionEvent e) -> {
            Homepage.Revisestage.close();
        });
        modify.setOnAction((ActionEvent e)->{
            ReavmpMessage(student_message);
            Homepage.Revisestage.close();
        });


        Homepage.Revisestage.setTitle("查询成功");
        Homepage.Revisestage.setScene(scene);
        Homepage.Revisestage.getIcons().add(new Image("File:title/find.png"));
//        stage.initStyle(StageStyle.UTILITY);

        /************测试输出************/
//        System.out.println(Message_gridPane.getMassage());

        Homepage.Revisestage.show();
    }




    /**修改学生失败页面*/
    private static void Changeerror() {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(Homepage.class.getResource("cinerror.fxml"));
            Scene scene1 = new Scene(fxmlLoader.load());
            Hintstage.setTitle("修改失败");
            Hintstage.getIcons().add(new Image("File:title/error.png"));
            //stage.initOwner();需要禁用主窗口
            Hintstage.setResizable(false);
            Hintstage.setScene(scene1);
            Hintstage.show();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    /**修改学生成功页面*/
    private static void Changuscceed() {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(Homepage.class.getResource("changsucceed.fxml"));
            Scene scene1 = new Scene(fxmlLoader.load());
            Hintstage.setTitle("修改成功");
            Hintstage.getIcons().add(new Image("File:title/check.png"));
            //stage.initOwner();需要禁用主窗口
            Hintstage.setResizable(false);
            Hintstage.setScene(scene1);
            Hintstage.show();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    /**添加学生失败信息不全页面*/
    public static void Cinerror(){
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(Homepage.class.getResource("cinerror.fxml"));
            Scene scene1 = new Scene(fxmlLoader.load());
            Hintstage.setTitle("添加失败");
            Hintstage.getIcons().add(new Image("File:title/error.png"));
            //stage.initOwner();需要禁用主窗口
            Hintstage.setResizable(false);
            Hintstage.setScene(scene1);
            Hintstage.show();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    /**添加学生失败已存在页面*/
    public static void Adderror(){
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(Homepage.class.getResource("adderror.fxml"));
            Scene scene1 = new Scene(fxmlLoader.load());
            Hintstage.setTitle("添加失败");
            Hintstage.getIcons().add(new Image("File:title/error.png"));
            //stage.initOwner();需要禁用主窗口
            Hintstage.setResizable(false);
            Hintstage.setScene(scene1);
            Hintstage.show();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    /**添加学生成功页面*/
    public static void Cinsucceed(){
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(Homepage.class.getResource("cinsucceed.fxml"));
            Scene scene1 = new Scene(fxmlLoader.load());
            Hintstage.setTitle("添加成功");
            Hintstage.getIcons().add(new Image("File:title/check.png"));
            //stage.initOwner();需要禁用主窗口
            Hintstage.setResizable(false);
            Hintstage.setScene(scene1);
            Hintstage.show();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

}
